var firstMissingPositive = function(nums) {
    nums.sort((a, b) => { return a - b });
    if (nums[0] > 1) {
        console.log("1");
        return 1;
    }
    let l = 0;
    let r = 1;
    for (let i = 0; i < nums.length; i++) {
        if (nums[l] == nums[r] - 1 && r <= nums.length - 1 || nums[l] <= 0) {
            l++;
            r++;
            // console.log(r);
        } else {
            console.log(nums[l] + 1);
            return nums[l] + 1;

        }
        if (r == nums.length - 1) {
            console.log(nums[r] + 1);
            return nums[r] + 1;
        }
    }
};

const a = [3, 4, -1, 1];

firstMissingPositive(a);